#include "tcpclient.h"
#include <QDebug>
TcpClient::TcpClient(QObject *parent)
    : TcpBase{parent}{
    source = FromClient;
}

bool TcpClient::connectToHost(const QHostAddress &address, quint16 port){
    if(socket->state() == QAbstractSocket::UnconnectedState){
        //主动和服务器进行连接
        socket->connectToHost(address, port);
        if (socket->waitForConnected(1000))  // 连接成功则进入if{}
        {
            connect(socket, SIGNAL(readyRead()), this, SLOT(onRecvMsg()));
            connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(socketErrorHandler(QAbstractSocket::SocketError)));
            // QString ip = socket->peerAddress().toString();
            // quint16 port = socket->peerPort();
            // CommonMessage msg = CommonMessage(NewConnection, ip+":"+QString::number((int)port));
            // emit sendMsgToMain(source, msg);
            return true;
        }
    }
    return false;
}
